package com.slwish.xty.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slwish.xty.R;
import com.slwish.xty.entity.CreditLifeModel;

import java.util.List;

/**
 * 模块说明：
 *
 * @author 陈浩
 * @date 2017/11/30
 */

public class CreditLifeAdapter extends BaseQuickAdapter<CreditLifeModel, BaseViewHolder> {
    private OnItemsClickListener listener;

    public CreditLifeAdapter(@Nullable List<CreditLifeModel> data) {
        super(R.layout.item_credit_life, data);
    }

    @Override
    protected void convert(final BaseViewHolder holder, CreditLifeModel item) {
        holder.setText(R.id.tv_title, item.getDesc());
        RecyclerView recyclerView = holder.getView(R.id.recyclerview);
        if (item.getList() != null && item.getList().size() > 0) {
            final CreditLifeChildAdapter imageAdapter = new CreditLifeChildAdapter(item.getList());
            recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
            recyclerView.setAdapter(imageAdapter);
            recyclerView.setVisibility(View.VISIBLE);
            imageAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                    if (listener != null)
                        listener.onItemsClick(adapter, view, holder.getAdapterPosition(),position);
//                    Toast.makeText(mContext, imageAdapter.getItem(position).getUrl() + "    " + position, Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            recyclerView.setVisibility(View.GONE);
        }
    }

    public interface OnItemsClickListener {
        void onItemsClick(BaseQuickAdapter adapter, View view, int parentPosition, int position);
    }

    public void setOnItemsClickListener(OnItemsClickListener listener) {
        this.listener = listener;
    }
}
